package com.dayu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.dayu.gulimall.product.entity.ProductAttrValueEntity;
import com.dayu.gulimall.product.service.ProductAttrValueService;
import com.dayu.gulimall.product.vo.AttrRespVo;
import com.dayu.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.dayu.gulimall.product.service.AttrService;
import com.dayu.common.utils.PageUtils;
import com.dayu.common.utils.R;


/**
 * 商品属性
 *
 * @author dayu
 * @email dayu@foxmail.com
 * @date 2020-05-07 09:56:16
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 获取分类规格参数
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId, @PathVariable("attrType") String type) {
        PageUtils page = attrService.querybaseAttrPage(params, catelogId, type);
        return R.ok().put("page", page);
    }

//    /**
//     * 获取分类规格参数
//     */
//    @RequestMapping("/base/list/{catelogId}")
//    //@RequiresPermissions("product:attr:list")
//    public R baseList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
//        PageUtils page = attrService.queryBaseListPage(params, catelogId);
//        return R.ok().put("page", page);
//    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 查询属性详情信息
     */
    @RequestMapping("/info/{attrId}")
    // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        //		AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr) {
        attrService.saveAttrVo(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr) {
        attrService.updateAttr(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }


    /**
     * /product/attr/base/listforspu/{spuId}
     */
    @GetMapping("/base/listforspu/{spuId}")
    // @RequiresPermissions("product:attr:delete")
    public R listForspu(@PathVariable Long spuId) {
        List<ProductAttrValueEntity> list = productAttrValueService.listForspu(spuId);
        return R.ok().put("data", list);
    }


}
